﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CSSAdriverapp
{
    /// <summary>
    /// Interaction logic for EditVehicle.xaml
    /// </summary>
    public partial class EditVehicle : Window
    {
        public EditVehicle()
        {
            InitializeComponent();
            populateNameGrid();
            setuptimepicker();

        }

        public void populateNameGrid()
        {
            DataClassesDataContext db = new DataClassesDataContext();
            IQueryable<Vehicle> vehicles = from c in db.Vehicles select c;
            vehicles.ToList();
            vehicleNameDataGrid.ItemsSource = vehicles;
        }

        public void populateTimeGrid()
        {
            if (vehicleNameDataGrid.SelectedItem != null)
            {
                DataClassesDataContext db = new DataClassesDataContext();
                IQueryable<VehicleAvailability> vehicleavailability = from c in db.VehicleAvailabilities where c.vehicleID == ((Vehicle)(vehicleNameDataGrid.SelectedItem)).ID select c;
                vehicleavailability.ToList();
                timeDataGrid.ItemsSource = vehicleavailability;
            }
        }

        public void setuptimepicker()
        {
            List<string> day = new List<string>();
            //Days of week for combo box
            day.Add("Sunday");
            day.Add("Monday");
            day.Add("Tuesday");
            day.Add("Wednesday");
            day.Add("Thursday");
            day.Add("Friday");
            day.Add("Saturday");
            dayComboBox.ItemsSource = day;

            //Hours for combo box
            List<string> hour = new List<string>();
            for (int i = 0; i < 12; i++)
            {
                int z = i + 1;
                hour.Add(z.ToString());
            }
            startHourComboBox.ItemsSource = hour;
            endHourComboBox.ItemsSource = hour;

            //Minutes for combo box
            List<string> min = new List<string>();
            for (int i = 0; i < 2; i++)
            {
                int z = i * 5;
                min.Add(0 + z.ToString());
            }
            for (int i = 2; i < 12; i++)
            {
                int z = i * 5;
                min.Add(z.ToString());
            }
            startMinuteComboBox.ItemsSource = min;
            endMinuteComboBox.ItemsSource = min;

            //am pm for combobox
            List<string> ampm = new List<string>();
            ampm.Add("PM");
            ampm.Add("AM");
            startAmPmComboBox.ItemsSource = ampm;
            endAmPmComboBox.ItemsSource = ampm;
        }

        public List<Vehicle> vehicles
        {
            get;
            set;
        }

        public List<VehicleAvailability> vehicleavailability
        {
            get;
            set;
        }

        public List<String> day
        {
            get;
            set;
        }

        public List<String> hour
        {
            get;
            set;
        }
        public List<String> min
        {
            get;
            set;
        }
        public List<String> ampm
        {
            get;
            set;
        }

        private void vehicleNameDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (vehicleNameDataGrid.SelectedItem != null)
            {
                nameLabel.Content = ((Vehicle)(vehicleNameDataGrid.SelectedItem)).Name;

                if ((((Vehicle)(vehicleNameDataGrid.SelectedItem)).Unavailable == false)
                    || (((Vehicle)(vehicleNameDataGrid.SelectedItem)).Unavailable == null))
                {
                    markUnavailableButton.Content = "Mark Unavailable";
                    hideAvailabilityRectangle.Visibility = System.Windows.Visibility.Hidden;
                    unavailabilityLabel.Visibility = System.Windows.Visibility.Hidden;
                }
                else
                {
                    hideAvailabilityRectangle.Visibility = System.Windows.Visibility.Visible;
                    unavailabilityLabel.Visibility = System.Windows.Visibility.Visible;
                    markUnavailableButton.Content = "Mark Available";
                }
                populateTimeGrid();
            }
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            if (vehicleNameDataGrid.SelectedItem != null && startHourComboBox.SelectedValue != null
                && startMinuteComboBox.SelectedValue != null && endHourComboBox.SelectedValue != null && endMinuteComboBox.SelectedValue != null
                && dayComboBox.SelectedValue != null && startAmPmComboBox.SelectedValue != null && endAmPmComboBox.SelectedValue != null)
            {
                missingFieldLabel.Visibility = System.Windows.Visibility.Hidden;
                fillt();
            }
            else
            {
                missingFieldLabel.Visibility = System.Windows.Visibility.Visible;
            }   
        }

        public void fillt()
        {
            int starth, startm, endh, endm = new int();

                if (startAmPmComboBox.Text == "PM")
                {
                    starth = int.Parse(startHourComboBox.Text.ToString()) + 12;
                }
                else
                {
                    starth = int.Parse(startHourComboBox.Text.ToString());
                }
                if (endAmPmComboBox.Text == "PM")
                {
                    endh = int.Parse(endHourComboBox.Text.ToString()) + 12;
                }
                else
                {
                    endh = int.Parse(endHourComboBox.Text.ToString()); 
                }                                           
            
                startm = int.Parse(startMinuteComboBox.Text.ToString());
                endm = int.Parse(endMinuteComboBox.Text.ToString());


            DataClassesDataContext db = new DataClassesDataContext();
            VehicleAvailability addavail = new VehicleAvailability
            {
                vehicleID = ((Vehicle)(vehicleNameDataGrid.SelectedItem)).ID,
                day = dayComboBox.SelectedItem.ToString(),
                starttime = new DateTime(1991, 2, 26, starth, startm, 0),
                endtime = new DateTime(1991, 2, 26, endh, endm, 0),
                
            };

            // Add the new object to the Orders collection.
            db.VehicleAvailabilities.InsertOnSubmit(addavail);

            // Submit the change to the database.
            try
            {
                db.SubmitChanges();
                populateTimeGrid();
            }
            catch (Exception c)
            {
                Console.WriteLine(c);
                // Make some adjustments.
                // ...
                // Try again.
                db.SubmitChanges();
            }
        }

        private void deleteTimeButton_Click(object sender, RoutedEventArgs e)
        {
            if (timeDataGrid.SelectedItem != null)
            {
                DataClassesDataContext db = new DataClassesDataContext();
                // Query the database for the rows to be deleted.
                var deleteVA =
                    from details in db.VehicleAvailabilities
                    where details.ID == ((VehicleAvailability)(timeDataGrid.SelectedItem)).ID
                    select details;

                foreach (var detail in deleteVA)
                {
                    db.VehicleAvailabilities.DeleteOnSubmit(detail);
                }

                try
                {
                    db.SubmitChanges();
                    populateTimeGrid();
                }
                catch (Exception c)
                {
                    Console.WriteLine(c);
                    // Provide for exceptions.
                }
            }
        }

        private void markUnavailableButton_Click(object sender, RoutedEventArgs e)
        {
            if (vehicleNameDataGrid.SelectedItem != null)
            {
                DataClassesDataContext db = new DataClassesDataContext();
                // Query the database for the row to be updated.
                var query =
                    from veh in db.Vehicles
                    where veh.ID == ((Vehicle)(vehicleNameDataGrid.SelectedItem)).ID
                    select veh;

                // Execute the query, and change the column values
                // you want to change.
                foreach (Vehicle veh in query)
                {
                    if (veh.Unavailable == null)
                    {
                        veh.Unavailable = true;
                        markUnavailableButton.Content = "Mark Available";
                        hideAvailabilityRectangle.Visibility = System.Windows.Visibility.Visible;
                        unavailabilityLabel.Visibility = System.Windows.Visibility.Visible;
                        break;
                    }

                    if (veh.Unavailable == false)
                    {
                        veh.Unavailable = true;
                        markUnavailableButton.Content = "Mark Available";
                        hideAvailabilityRectangle.Visibility = System.Windows.Visibility.Visible;
                        unavailabilityLabel.Visibility = System.Windows.Visibility.Visible;
                        break;
                    }

                    if (veh.Unavailable == true)
                    {
                        veh.Unavailable = false;
                        markUnavailableButton.Content = "Mark Unavailable";
                        hideAvailabilityRectangle.Visibility = System.Windows.Visibility.Hidden;
                        unavailabilityLabel.Visibility = System.Windows.Visibility.Hidden;
                        break;
                    }
                    
                    // Insert any additional changes to column values.
                }

                // Submit the changes to the database.
                try
                {
                    db.SubmitChanges();
                    populateNameGrid();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    // Provide for exceptions.
                }
            }
        }

        private void deleteVehiclesButton_Click(object sender, RoutedEventArgs e)
        {
            if (vehicleNameDataGrid.SelectedItem != null)
            {
                DataClassesDataContext db = new DataClassesDataContext();
                // Query the database for the rows to be deleted.
                var deleteVA =
                    from details in db.Vehicles
                    where details.ID == ((Vehicle)(vehicleNameDataGrid.SelectedItem)).ID
                    select details;

                foreach (var detail in deleteVA)
                {
                    db.Vehicles.DeleteOnSubmit(detail);
                }

                try
                {
                    db.SubmitChanges();
                    populateNameGrid();
                }
                catch (Exception c)
                {
                    Console.WriteLine(c);
                    // Provide for exceptions.
                }
            }
        }

    }
}
